﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using AnalogClock;
using PaintEventArgs = System.Windows.Forms.PaintEventArgs;

namespace Clock
{
    public partial class DateForm : Form
    {
        public DateForm()
        {
            InitializeComponent();
        }

        public bool IsMouseHover { get; set; }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel=true;
            Hide();
        }

        private Font psmFont = new Font("Palace Script MT", 12f, FontStyle.Italic);
        private Font tahomaFont = new Font("Tahoma", 10f, FontStyle.Bold);
        private PointF zeroPoint = new PointF(0f, 0f);


        private void Clock1BackgroundPaint(object sender, PaintEventArgs e)
        {
            var sbr = new SolidBrush(clock1.ForeColor);
            SizeF sz = e.Graphics.MeasureString("ARMAN", psmFont, zeroPoint, StringFormat.GenericDefault);
            float x = clock1.CenterPoint.PivotalPoint.X - (sz.Width / 2f);
            var y = (float) (clock1.Radius*0.3);
            e.Graphics.DrawString("ARMAN", psmFont, sbr, x, y);
            sz = e.Graphics.MeasureString(clock1.Value.ToShortTimeString(), Font, zeroPoint, StringFormat.GenericDefault);
            x = clock1.CenterPoint.PivotalPoint.X - (sz.Width / 2f);
            y = (clock1.Rectangle.Height - ((float) (clock1.Radius*0.3))) - sz.Height;
            e.Graphics.DrawString(clock1.Value.ToLongTimeString(), Font, sbr, x, y);
            sbr.Dispose();
            string str = (clock1.Value.Month) + " " + clock1.Value.Day;
            sz = e.Graphics.MeasureString(str, tahomaFont, zeroPoint, StringFormat.GenericDefault);
            var rec = new Rectangle((int) Math.Round(((clock1.Rectangle.Width - (clock1.Radius*0.3)) - sz.Width)),
                                    (int) Math.Round((clock1.CenterPoint.PivotalPoint.Y - 7.5)),
                                    (int) Math.Round(sz.Width), (int) Math.Round(sz.Height));
            var br = new LinearGradientBrush(rec, SystemColors.ControlDark, Color.White, LinearGradientMode.Vertical);
            e.Graphics.FillRectangle(br, rec);
            br.Dispose();
            e.Graphics.DrawString(str, tahomaFont, Brushes.Black, rec, StringFormat.GenericTypographic);
            rec.Inflate(1, 1);
            e.Graphics.DrawRectangle(Pens.White, rec);
        }


        private void clock1_BigMarkerPainting(object sender, AnalogClock.PaintEventArgs e)
        {
            var m = (Marker) sender;
            var gp = (GraphicsPath) m.Path.Clone();
            var p = new Pen(Color.Gray, 2f);
            gp.Widen(p);
            p.Dispose();
            e.Graphics.FillPath(Brushes.LightGray, gp);
            gp.Dispose();
            if (m.Path.PointCount > 2)
            {
                if (false)
                {
                    var br = new PathGradientBrush(m.Path);
                    br.CenterColor = Color.White;
                    br.SurroundColors = new[] {m.Color};
                    e.Brush = br;
                    br.Dispose();
                }
                else if (false)
                {
                    var br = new HatchBrush(HatchStyle.DarkUpwardDiagonal, m.Color, Color.White);
                    e.Brush = br;
                    br.Dispose();
                }
                else if (true)
                {
                    var br = new LinearGradientBrush(m.Path.GetBounds(), m.Color, Color.White,
                                                     LinearGradientMode.BackwardDiagonal);
                    e.Brush = br;
                    br.Dispose();
                }
            }
        }


        private void clock1_CenterPointPainting(object sender, AnalogClock.PaintEventArgs e)
        {
            var c = (Center) sender;
            if (false)
            {
                var br = new PathGradientBrush(c.Path);
                br.WrapMode = WrapMode.Tile;
                br.CenterColor = Color.White;
                br.SurroundColors = new[] {c.Color};
                br.CenterPoint = c.PivotalPoint;
                e.Brush = br;
                br.Dispose();
            }
            else if (false)
            {
                var br = new HatchBrush(HatchStyle.LargeCheckerBoard, c.Color, Color.White);
                e.Brush = br;
                br.Dispose();
            }
            else if (true)
            {
                var br = new LinearGradientBrush(c.Path.GetBounds(), c.Color, Color.White,
                                                 LinearGradientMode.BackwardDiagonal);
                e.Brush = br;
                br.Dispose();
            }
        }


        private void clock1_CustomElementRequest(object sender, CustomElementEventArgs e)
        {
            if (sender.GetType() == typeof (Center))
            {
                float radius = clock1.CenterPoint.Radius;
                var gPath = new GraphicsPath();
                var points = new PointF[8];
                PointF pPoint = clock1.CenterPoint.PivotalPoint;
                var helper = (float) (radius*Math.Sin(0.78539816339744828));
                points[0] = new PointF(pPoint.X, pPoint.Y - radius);
                points[1] = new PointF(pPoint.X + helper, pPoint.Y - helper);
                points[2] = new PointF(pPoint.X + radius, pPoint.Y);
                points[3] = new PointF(pPoint.X + helper, pPoint.Y + helper);
                points[4] = new PointF(pPoint.X, pPoint.Y + radius);
                points[5] = new PointF(pPoint.X - helper, pPoint.Y + helper);
                points[6] = new PointF(pPoint.X - radius, pPoint.Y);
                points[7] = new PointF(pPoint.X - helper, pPoint.Y - helper);
                gPath.AddLines(points);
                gPath.CloseAllFigures();
                e.CustomPath = gPath;
            }
            else if (sender.GetType() == typeof (Hand))
            {
                var h = (Hand) sender;
                var points = new PointF[4];
                var gPath = new GraphicsPath();
                PointF pPoint = h.PivotalPoint;
                points[0] = new PointF(pPoint.X, pPoint.Y - h.PrimeLength);
                points[1] = new PointF(pPoint.X + (h.Width/2f), pPoint.Y);
                points[2] = new PointF(pPoint.X, pPoint.Y + h.TailLength);
                points[3] = new PointF(pPoint.X - (h.Width/2f), pPoint.Y);
                gPath.AddLines(points);
                gPath.CloseAllFigures();
                e.CustomPath = gPath;
            }
            else if (sender.GetType() == typeof (Marker))
            {
                var m = (Marker) sender;
                var points = new PointF[3];
                var gPath = new GraphicsPath();
                PointF pPoint = m.PivotalPoint;
                var pOuter = new PointF(pPoint.X, clock1.Radius - m.Radius);
                var pInner = new PointF(pPoint.X, clock1.Radius - (m.Radius - m.Length));
                points[0] = new PointF(pOuter.X - (m.Width/2f), pOuter.Y);
                points[1] = new PointF(pOuter.X + (m.Width/2f), pOuter.Y);
                points[2] = pInner;
                gPath.AddLines(points);
                gPath.CloseAllFigures();
                e.CustomPath = gPath;
            }
        }

        private void clock1_HandPainting(object sender, AnalogClock.PaintEventArgs e)
        {
            var h = (Hand) sender;
            var gp = (GraphicsPath) h.Path.Clone();
            var p = new Pen(Color.Gray, 2f);
            gp.Widen(p);
            p.Dispose();
            e.Graphics.FillPath(Brushes.Gray, gp);
            gp.Dispose();
            if (h.Path.PointCount > 2)
            {
                if (false)
                {
                    var br = new PathGradientBrush(h.Path);
                    br.CenterColor = Color.White;
                    br.SurroundColors = new[] {h.Color};
                    e.Brush = br;
                    br.Dispose();
                }
                else if (false)
                {
                    var br = new HatchBrush(HatchStyle.DarkUpwardDiagonal, h.Color, Color.White);
                    e.Brush = br;
                    br.Dispose();
                }
                else if (true)
                {
                    var br = new LinearGradientBrush(h.Path.GetBounds(), h.Color, Color.White,
                                                     LinearGradientMode.BackwardDiagonal);
                    e.Brush = br;
                    br.Dispose();
                }
            }
        }
    }
}